package com.linktrip.devicemanager.util;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.linktrip.devicemanager.entity.DeviceInfo;

import io.netty.channel.ChannelHandlerContext;

public class DevicesListHelper {
	Logger log = LoggerFactory.getLogger(DevicesListHelper.class);
	public static byte[] CMD_UNLOCK_DATA = { 0x36, 0x00, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00, 0x24, 0x47, (byte) 0x9c, 0x04,
			0x56, (byte) 0x97, (byte) 0xaa, (byte) 0xd1, 0x04, 0x01, 0x02, 0x46, 0x11, 0x00, 0x00, 0x03, 0x2a,
			(byte) 0xba, 0x3f, 0x28, 0x39, 0x19, 0x3f, 0x40, 0x5a, 0x57, (byte) 0xbb, (byte) 0xdc, (byte) 0x96, 0x60,
			0x5e, 0x40, (byte) 0xe1, 0x07, 0x02, 0x0b, 0x0e, 0x32, 0x37, 0x00, 0x01, 0x13, 0x2b, 0x23, 0x27, 0x00, 0x00,
			0x00 };
	public static byte[] CMD_REQUEST = { 0x36, 0x00, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00, 0x24, 0x47, (byte) 0x9c, 0x04,
			0x56, (byte) 0x97, (byte) 0xaa, (byte) 0xd1, 0x04, 0x01, 0x00, 0x46, 0x11, 0x00, 0x00, 0x03, 0x2a,
			(byte) 0xba, 0x3f, 0x28, 0x39, 0x19, 0x3f, 0x40, 0x5a, 0x57, (byte) 0xbb, (byte) 0xdc, (byte) 0x96, 0x60,
			0x5e, 0x40, (byte) 0xe1, 0x07, 0x02, 0x0b, 0x0e, 0x32, 0x37, 0x00, 0x01, 0x13, 0x2b, 0x23, 0x27, 0x00, 0x00,
			0x00 };

	private static Map<String, ChannelHandlerContext> map_nio = new ConcurrentHashMap<String, ChannelHandlerContext>();
	private static Map<String, Integer> response_map = new ConcurrentHashMap<String, Integer>();
	private static Map<String, DeviceInfo> DEVICES = new ConcurrentHashMap<String, DeviceInfo>();
	private static DevicesListHelper devicesListHelper;

	private DevicesListHelper() {
	}

	public static synchronized DevicesListHelper getSingleton() {
		if (null == devicesListHelper)
			devicesListHelper = new DevicesListHelper();
		return devicesListHelper;
	}


	public void addIntoMap(String key, ChannelHandlerContext soc) {
		log.debug("put into map  key:" + key + " value:" + soc);
		if (ToolKits.isEmpty(key))
			return;
			map_nio.remove(key);
			map_nio.put(key, soc);
	}

	public void removeFromMap(String id) {
		if (null != map_nio)
			map_nio.remove(id);
	}

	public Map<String, ChannelHandlerContext> getDevicesMap_nio() {
		return map_nio;
	}

	public void putMessage(String key, Integer value) {
		if (ToolKits.isEmpty(key))
			return;
			response_map.remove(key);
			response_map.put(key, value);
	}

	public void removeMessage(String key) {
		response_map.remove(key);
	}

	public Integer getMessageByKey(String key) {
		Integer result = response_map.get(key);
		
		if(result == null) result = 0;
		return result;
	}
	
	public void putDeviceInfo(String key,DeviceInfo info){
		DEVICES.put(key, info);
	}
	
	public DeviceInfo getDeviceInfo(String key){
		return DEVICES.get(key);
	}

}
